//
//  YsFileBrowserVideoVC.m
//  YsFileBrowser
//
//  Created by 宋宏康 on 2020/3/17.
//

#import "YsFileBrowserVideoVC.h"
#import <ZFPlayer/ZFPlayer.h>
#import <ZFPlayer/ZFAVPlayerManager.h>
#import <ZFPlayer/ZFPlayerControlView.h>
#import <ZFPlayer/UIView+ZFFrame.h>
#import <ZFPlayer/UIImageView+ZFCache.h>
#import <ZFPlayer/ZFUtilities.h>
#import <Masonry/Masonry.h>
#import <ReactiveObjC/ReactiveObjC.h>

static NSString *kVideoCover = @"https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";


@interface YsFileBrowserVideoVC ()
@property (nonatomic, strong) NSMutableArray<NSString *> *dataSource;
@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) UIImageView *containerView;
@property (nonatomic, strong) ZFPlayerControlView *controlView;
@property (nonatomic, strong) NSArray <NSURL *>*assetURLs;
@end

@implementation YsFileBrowserVideoVC

@synthesize dataSource = _dataSource;

- (instancetype)initWithDataSource:(NSArray <NSString *>*)dataSource
{
    self = [super init];
    if (self) {
        [self.dataSource setArray:dataSource];
//        [self.dataSource enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            [self.assetURLs addObject:[NSURL fileURLWithPath:obj]];
//        }];
    }
    return self;
}

- (void)setDataSource:(NSMutableArray<NSString *> *)dataSource
{
    _dataSource = dataSource;
    
//    [self.player playTheIndex:0];
}

- (NSMutableArray<NSString *> *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
    
    [self.view addSubview:self.containerView];
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.top.mas_equalTo(self.view.mas_safeAreaLayoutGuideTop);
        } else {
            // Fallback on earlier versions
            make.top.mas_equalTo(self.view.mas_top);
        }
        make.left.right.bottom.mas_equalTo(0);
//        make.height.mas_equalTo(200);
    }];
    
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    ZFPlayerController *player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
    self.player = player;
    self.player.controlView = self.controlView;
    self.player.assetURLs = self.assetURLs;
    
    [self.player playTheIndex:0];
    
    @weakify(self);
       ///当前控制器将要出现
        [[self rac_signalForSelector:@selector(viewWillAppear:)]
         subscribeNext:^(RACTuple * _Nullable x) {
            @strongify(self);
            self.player.viewControllerDisappear = NO;
        }];
        
        ///当前控制器将要消失
        [[self rac_signalForSelector:@selector(viewWillDisappear:)]
        subscribeNext:^(RACTuple * _Nullable x) {
            @strongify(self);
            self.player.viewControllerDisappear = YES;
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.player enterFullScreen:YES animated:YES];
        });
    
       self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
           @strongify(self)
           /// 解决导航栏上移问题
           if (!isFullScreen) {
               [self dismissViewControllerAnimated:YES completion:nil];
           }
       };
}

- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        _controlView.fastViewAnimated = YES;
        _controlView.autoHiddenTimeInterval = 5;
        _controlView.autoFadeTimeInterval = 0.5;
        _controlView.prepareShowLoading = YES;
        _controlView.prepareShowControlView = YES;
    }
    return _controlView;
}

- (UIImageView *)containerView {
    if (!_containerView) {
        _containerView = [UIImageView new];
        [_containerView setImageWithURLString:kVideoCover placeholder:[ZFUtilities imageWithColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1] size:CGSizeMake(1, 1)]];
    }
    return _containerView;
}

- (NSArray<NSURL *> *)assetURLs {
    if (!_assetURLs) {
        [self.dataSource enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            _assetURLs = @[[NSURL fileURLWithPath:obj]];
        }];
    }
    return _assetURLs;
}

- (void)dealloc
{
    NSLog(@"%s",__func__);
}
@end
